﻿using IDEAMS.core;
using IDEAMS.utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;


namespace IDEAMS.model.setting
{
    public class GlobalSetting
    {
        private static ILog logger = LogManager.GetLogger(typeof(GlobalSetting));

        /// <summary>
        /// 报警音开关
        /// </summary>
        public bool alarmSoundOn;
        /// <summary>
        /// 阈值
        /// </summary>
        public ThresholdSetting threshold;

        /// <summary>
        /// 烤机时间
        /// </summary>
        public double continuousRunTime;

        /// <summary>
        /// 检验顺序
        /// </summary>
        public List<int> inspectionSequences;

        public GlobalSetting()
        {
            //Reset();
        }

        public GlobalSetting(GlobalSetting setting) : this()
        {
            Copy(setting);
        }

        public void Reset()
        {
            alarmSoundOn = true;
            threshold = new ThresholdSetting();
            continuousRunTime = 72;
            inspectionSequences = new List<int>();
            inspectionSequences.Add((int)ProcessStep.PRE_DATA_ACQUISITION);
            inspectionSequences.Add((int)ProcessStep.PRE_ETALON);
            inspectionSequences.Add((int)ProcessStep.PRE_SPECTRUM_GRATING);
            inspectionSequences.Add((int)ProcessStep.PRE_SPECTRUM_NO_GRATING);
            inspectionSequences.Add((int)ProcessStep.CONTINUOUS_DETECTION);
            inspectionSequences.Add((int)ProcessStep.POST_SPECTRUM_NO_GRATING);
            inspectionSequences.Add((int)ProcessStep.POST_SPECTRUM_GRATING);
            inspectionSequences.Add((int)ProcessStep.POST_ETALON);
            inspectionSequences.Add((int)ProcessStep.POST_DATA_ACQUISITION);
        }

        public void Copy(GlobalSetting setting)
        {
            this.alarmSoundOn = setting.alarmSoundOn;
            threshold = setting.threshold.Clone();
            continuousRunTime = setting.continuousRunTime;
            inspectionSequences = new List<int>();
            foreach (var item in setting.inspectionSequences)
            {
                inspectionSequences.Add(item);
            }
        }

        public GlobalSetting Clone()
        {
            return new GlobalSetting(this);
        }

        public int Load()
        {
            int rn = 0;

            string settingFile = TW_App.fileManager.globalSettingFile;
            bool existsFlag = File.Exists(settingFile);
            if (!existsFlag)
            {
                Reset();
                logger.WarnFormat("全局设置文件[{0}]不存在", settingFile);
                return -2;
            }

            GlobalSetting setting = XmlUtil.Load<GlobalSetting>(settingFile);
            if (setting != null)
            {
                //todo:设置变量
                Copy(setting);
                rn = 0;
            }
            else
            {
                Reset();
                logger.ErrorFormat("加载全局设置[{0}]失败", settingFile);
                rn = -3;
            }

            return rn;
        }

        public bool Save()
        {
            String settingFile = TW_App.fileManager.globalSettingFile;

            GlobalSetting globalSetting = new GlobalSetting(this);
            bool rn = XmlUtil.Save<GlobalSetting>(settingFile, globalSetting);
            return rn;
        }

        public int Load(string serialNo)
        {
            int rn = 0;

            string settingFile = string.Format(TW_App.fileManager.deviceSettingFile, serialNo);
            bool existsFlag = File.Exists(settingFile);
            if (!existsFlag)
            {
                Reset();
                logger.WarnFormat("全局设置文件[{0}]不存在", settingFile);
                return -2;
            }

            GlobalSetting setting = XmlUtil.Load<GlobalSetting>(settingFile);
            if (setting != null)
            {
                //todo:设置变量
                Copy(setting);
                rn = 0;
            }
            else
            {
                Reset();
                logger.ErrorFormat("加载全局设置[{0}]失败", settingFile);
                rn = -3;
            }

            return rn;
        }

        public bool Save(string serialNo)
        {
            string settingFile = string.Format(TW_App.fileManager.deviceSettingFile,serialNo);

            GlobalSetting globalSetting = new GlobalSetting(this);
            bool rn = XmlUtil.Save<GlobalSetting>(settingFile, globalSetting);
            return rn;
        }
    }
}
